package com.hy.mvc.web.conroller.doc;

import com.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Nullable;
import java.lang.annotation.Annotation;
import java.util.ArrayList;

/**
 * <pre>
 *    author  : Booker
 *    time    : 2017/12/21 22:46
 *    desc    : show the function of this class
 *    version : v1.0
 * </pre>
 */
@Configuration // 配置注解
@EnableWebMvc // 启用Mvc //非springboot框架需要引入注解@EnableWebMvc
@EnableSwagger2
@ComponentScan("com.hy.mvc.web.conroller") //需要扫描的包路径
public class SwaggerConfig {

    @Value("${apiEnabled}")
    private boolean apiEnabled;

    @Bean
    public Docket customDocket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
        docket.select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any())
                .build();
        return docket;
    }


    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                "Web-Mvc 接口文档",
                "Web-Mvc自用文档",
                "My Apps API terms of service",
                "503201142@qq.com",
                contact(),
                "no license",
                "no url",
                new ArrayList<VendorExtension>());

        return apiInfo;
    }

    private Predicate<RequestHandler> predicates(final Class<? extends Annotation>... annotations) {
        return new Predicate<RequestHandler>() {
            @Override
            public boolean apply(@Nullable RequestHandler input) {
                if (!apiEnabled) {
                    return false;
                }
                for (Class<? extends Annotation> annotation : annotations) {
                    if (input.isAnnotatedWith(annotation)) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public boolean test(@Nullable RequestHandler input) {
                return false;
            }
        };
    }

    private Contact contact() {
        return new Contact("nvc-web", "127.0.0.1:8080", "503201142@qq.com");
    }


}
